
package finalproject.server;


import java.util.List;

import javax.jdo.PersistenceManager;

import finalproject.server.PmfSingleton;
import finalproject.shared.Docent;
import finalproject.shared.Persoon;


public class DocentDAOPmf implements DocentDAO {

	
	public void addDocent(Docent docent) {
		PersistenceManager pm = PmfSingleton.get().getPersistenceManager();
		try {
			
			Persoon persoon = docent.getPersoon();
			pm.makePersistent(docent);
		} finally {
			pm.close();
		}
	}

	
	@SuppressWarnings("unchecked")
	public List<Docent> listDocenten() {
		PersistenceManager pm = PmfSingleton.get().getPersistenceManager();
		String query = "select from " + Docent.class.getName();
		return (List<Docent>) pm.newQuery(query).execute();
	}

	
	public void removeDocent(Docent docent) {
		PersistenceManager pm =PmfSingleton.get().getPersistenceManager();
		try {
			pm.currentTransaction().begin();

			docent = pm.getObjectById(Docent.class, docent.getDocentNr());
			pm.deletePersistent(docent);

			pm.currentTransaction().commit();
		} catch (Exception ex) {
			pm.currentTransaction().rollback();
			throw new RuntimeException(ex);
		} finally {
			pm.close();
		}
	}

	public void updateDocent(Docent docent) {
		PersistenceManager pm = PmfSingleton.get().getPersistenceManager();
		
		Persoon persoon = docent.getPersoon();
		
		String naam = persoon.getNaam();
		
		
		String kamer = docent.getKamer();
		String email = docent.getEmail();


		try {
			pm.currentTransaction().begin();
			
			docent = pm.getObjectById(Docent.class, docent.getDocentNr());
			persoon = docent.getPersoon();
			persoon.setNaam(naam);
			docent.setPersoon(persoon);
			docent.setKamer(kamer);
			docent.setEmail(email);

			pm.makePersistent(docent);
			pm.currentTransaction().commit();
		} catch (Exception ex) {
			pm.currentTransaction().rollback();
			throw new RuntimeException(ex);
		} finally {
			pm.close();
		}
	}

}
